import axios from "axios";
import utils from '@/common/utils'

//处理admin后台管理的请求
const adminApi = {
    //登录
    admin_login(username, password) {
        return ajax('/api/admin/login', 'post', {
            data: {
                username,
                password
            }
        })
    },
    admin_logout() {
        return ajax('/api/admin/logout', 'get')
    },

    //Dashboard工作台
    admin_getDashboardInfo() {
        return ajax('/api/admin/dashboard/get-dashboard-info', 'get')
    },
    getSessions(data) {
        return ajax('/api/admin/dashboard/get-sessions', 'post', {
            data
        })
    },

    //获取用户列表
    admin_getUserList(currentPage, limit, keyword, onlyAdmin) {
        let params = {currentPage, limit}
        if (keyword) {
            params.keyword = keyword
        }
        params.onlyAdmin = onlyAdmin
        return ajax('/api/admin/user/get-user-list', 'get', {
            params: params
        })
    },

    //用户管理
    admin_editUser(data) {
        return ajax('/api/admin/user/edit-user', 'put', {
            data
        })
    },
    admin_deleteUsers(ids) {
        return ajax('/api/admin/user/delete-user', 'delete', {
            data: {ids}
        })
    },
    admin_importUsers(users) {
        return ajax('/api/admin/user/insert-batch-user', 'post', {
            data: {
                users
            }
        })
    },
    admin_generateUser(data) {
        return ajax('/api/admin/user/generate-user', 'post', {
            data
        })
    },

    //获取数据后台服务和nacos相关详情
    admin_getGeneralSystemInfo() {
        return ajax('/api/admin/config/get-service-info', 'get')
    },

    getJudgeServer() {
        return ajax('/api/admin/config/get-judge-service-info', 'get')
    },

    //获取公告列表
    admin_getAnnouncementList(currentPage, limit) {
        return ajax('/api/admin/announcement', 'get', {
            params: {
                currentPage,
                limit
            }
        })
    },
    //修改公告
    admin_updateAnnouncement(data) {
        return ajax('/api/admin/announcement', 'put', {
            data
        })
    },
    //添加公告
    admin_createAnnouncement(data) {
        return ajax('/api/admin/announcement', 'post', {
            data
        })
    },
    //删除公告
    admin_deleteAnnouncement(aid) {
        return ajax('/api/admin/announcement', 'delete', {
            params: {
                aid
            }
        })
    },

    //获取通知列表
    admin_getNoticeList(currentPage, limit, type) {
        return ajax('/api/admin/msg/notice', 'get', {
            params: {
                currentPage,
                limit,
                type
            }
        })
    },
    //删除通知
    admin_deleteNotice(id) {
        return ajax('/api/admin/msg/notice', 'delete', {
            params: {
                id
            }
        })
    },
    //修改通知
    admin_updateNotice(data) {
        return ajax('/api/admin/msg/notice', 'put', {
            data
        })
    },
    //添加通知
    admin_createNotice(data) {
        return ajax('/api/admin/msg/notice', 'post', {
            data
        })
    },

    //系统配置
    admin_getWebsiteConfig() {
        return ajax('/api/admin/config/get-web-config', 'get')
    },
    admin_editWebsiteConfig(data) {
        return ajax('/api/admin/config/set-web-config', 'put', {
            data
        })
    },
    admin_getSMTPConfig() {
        return ajax('/api/admin/config/get-email-config', 'get')
    },
    admin_editSMTPConfig(data) {
        return ajax('/api/admin/config/set-email-config', 'put', {
            data
        })
    },
    admin_testSMTPConfig(email) {
        return ajax('/api/admin/config/test-email', 'post', {
            data: {
                email
            }
        })
    },
    admin_getDataBaseConfig() {
        return ajax('/api/admin/config/get-db-and-redis-config', 'get')
    },
    admin_editDataBaseConfig(data) {
        return ajax('/api/admin/config/set-db-and-redis-config', 'put', {
            data
        })
    },
    admin_deleteHomeCarousel(id) {
        return ajax('/api/admin/config/home-carousel', 'delete', {
            params: {
                id
            }
        })
    },
    //系统开关
    admin_getSwitchConfig() {
        return ajax('/api/admin/switch/info', 'get')
    },
    admin_saveSwitchConfig(data) {
        return ajax('/api/admin/switch/update', 'put', {
            data
        })
    },

    //讨论相关
    admin_updateDiscussion(data) {
        return ajax("/api/admin/discussion", 'put', {
            data
        })
    },
    admin_deleteDiscussion(data) {
        return ajax("/api/admin/discussion", 'delete', {
            data
        })
    },
    admin_getDiscussionReport(currentPage, limit) {
        return ajax("/api/admin/discussion-report", 'get', {
            params: {
                currentPage,
                limit
            }
        })
    },
    admin_updateDiscussionReport(data) {
        return ajax("/api/admin/discussion-report", 'put', {
            data
        })
    },

    //题目标签相关
    admin_addTag(data) {
        return ajax('/api/admin/tag', 'post', {
            data
        })
    },
    admin_updateTag(data) {
        return ajax('/api/admin/tag', 'put', {
            data
        })
    },
    admin_deleteTag(tid) {
        return ajax('/api/admin/tag', 'delete', {
            params: {
                tid
            }
        })
    },
    admin_getTagClassification() {
        return ajax('/api/admin/tag/classification', 'get')
    },
    admin_addTagClassification(data) {
        return ajax('/api/admin/tag/classification', 'post', {
            data
        })
    },
    admin_updateTagClassification(data) {
        return ajax('/api/admin/tag/classification', 'put', {
            data
        })
    },
    admin_deleteTagClassification(tcid) {
        return ajax('/api/admin/tag/classification', 'delete', {
            params: {
                tcid
            }
        })
    },

    //题目相关
    admin_getAllProblemTagList() {
        return ajax('/api/get-all-problem-tags', 'get')
    },
    admin_getProblemList(params) {
        params = utils.filterEmptyValue(params)
        return ajax('/api/admin/problem/get-problem-list', 'get', {
            params
        })
    },
    admin_createProblem(data) {
        return ajax('/api/admin/problem', 'post', {
            data
        })
    },
    admin_editProblem(data) {
        return ajax('/api/admin/problem', 'put', {
            data
        })
    },
    admin_deleteProblem(pid) {
        return ajax('/api/admin/problem', 'delete', {
            params: {
                pid
            }
        })
    },
    admin_changeProblemAuth(data) {
        return ajax('/api/admin/problem/change-problem-auth', 'put', {
            data
        })
    },
    admin_getProblem(pid) {
        return ajax('/api/admin/problem', 'get', {
            params: {
                pid
            }
        })
    },
    admin_getProblemTags(pid) {
        return ajax('/api/get-problem-tags', 'get', {
            params: {
                pid
            }
        })
    },
    admin_getProblemCases(pid, isUpload) {
        return ajax('/api/admin/problem/get-problem-cases', 'get', {
            params: {
                pid,
                isUpload
            }
        })
    },
    compileSPJ(data) {
        return ajax('/api/admin/problem/compile-spj', 'post', {
            data
        })
    },
    compileInteractive(data) {
        return ajax('/api/admin/problem/compile-interactive', 'post', {
            data
        })
    },


}

export default adminApi

/**
 * @param url
 * @param method get|post|put|delete...
 * @param params like queryString. if a url is index?a=1&b=2, params = {a: '1', b: '2'}
 * @param data post data, use for method put|post
 * @returns {axios}
 */
function ajax(url, method, options) {
    if (options !== undefined) {
        var {params = {}, data = {}} = options
    } else {
        params = data = {}
    }
    return new Promise((resolve, reject) => {
        axios({
            url,
            method,
            params,
            data
        }).then((res) => {
            resolve(res)
        }).catch(error => {
            reject(error)
        })
    })
}